class CliqueController {

    def index = { redirect(action: list, params: params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [cliqueList: Clique.list(params)]
    }

    def show = {
        def clique = Clique.get(params.id)

        if (!clique) {
            flash.message = "Clique not found with id ${params.id}"
            redirect(action: list)
        }
        else { return [clique: clique] }
    }

    def delete = {
        def clique = Clique.get(params.id)
        if (clique) {
            clique.delete()
            flash.message = "Clique ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = "Clique not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def edit = {
        def clique = Clique.get(params.id)

        if (!clique) {
            flash.message = "Clique not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            return [clique: clique]
        }
    }

    def update = {
        def clique = Clique.get(params.id)
        if (clique) {
            clique.properties = params

            if (params."person.id") {
                def person = Person.get(params."person.id")
                clique.addToMembers(person)
            }
            if (!clique.hasErrors() && clique.save()) {
                flash.message = "Clique ${params.id} updated"
                redirect(action: show, id: clique.id)
            }
            else {
                render(view: 'edit', model: [clique: clique])
            }
        }
        else {
            flash.message = "Clique not found with id ${params.id}"
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def clique = new Clique()
        clique.properties = params
        return ['clique': clique]
    }

    def save = {
        def clique = new Clique(params)
        if (!clique.hasErrors() && clique.save()) {
            flash.message = "Clique ${clique.id} created"
            redirect(action: show, id: clique.id)
        }
        else {
            render(view: 'create', model: [clique: clique])
        }
    }
}